//
//  SXOneViewController.m
//  SXDataManager_Example
//
//  Created by wangshuixin on 2019/1/23.
//  Copyright © 2019 wangshuixin. All rights reserved.
//

#import "SXOneViewController.h"
#import "SXDataManager-umbrella.h"
#import "SXSingletonClass.h"
#import "AFNetworking.h"

@interface SXOneViewController ()

@end

@implementation SXOneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor orangeColor];
    NSDictionary *data = @{@"name":@"x名字", @"age":@"18", @"score":@"100", @"tags":@[@"test", @"yes", @"fake", @"out!", @1, @2]};
    SXSingletonClass *model = [SXSingletonClass modelWithDictionary:data];
    NSLog(@"字典转模型 - 模型为：%@", model);
}
- (void)viewWillAppear:(BOOL)animated {
    // 请求本地服务器数据
    [SXOneViewController PostDataWithPath:@"http://192.168.1.4:8080/api/login" parameters:@{@"password":@"123456", @"username":@"abcd"} progress:^(NSProgress *downloadProgress) {
        
    } success:^(id obj) {
        NSLog(@"golang请求post返回数据：%@", obj);
    } failure:^(NSError *error) {
        
    }];
}
static AFHTTPSessionManager *manager;
+ (AFHTTPSessionManager *)sharedHttpSessionManager {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [AFHTTPSessionManager manager];
        manager.requestSerializer.timeoutInterval = 15.0;
    });
    
    return manager;
}

+ (void)PostDataWithPath:(NSString *)path
              parameters:(NSDictionary *)parameters
                progress:(void(^ __nullable)(NSProgress  *downloadProgress))download
                 success:(void(^)(id obj))success
                 failure:(void (^)(NSError *error))failure{
    
    AFHTTPSessionManager *manager = [self sharedHttpSessionManager];
    
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy];
    securityPolicy.validatesDomainName = NO;
    securityPolicy.allowInvalidCertificates = YES;
    manager.securityPolicy = securityPolicy;
    
    //    [manager.securityPolicy setAllowInvalidCertificates:NO];
    //    manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes= [NSSet setWithObjects:
                                                        @"application/json",
                                                        @"text/html",
                                                        @"text/json",
                                                        @"text/javascript",
                                                        @"text/plain" ,nil];
    
    NSString *finalUrl = [NSString stringWithFormat:@"%@",path];
    
    [manager POST:finalUrl parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        if (download != nil) {
            download(uploadProgress);
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        success(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
    
}

@end
